/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OCAll.java
 *
 * Created on Nov 8, 2010, 12:21:06 AM
 */

package lmclogistics;

import components.Image;
import components.Lightbox;
import components.NButton;
import components.Text;
import components.TextArea;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import sql.Sql;
/**
 *
 * @author Sonia Tan
 */
public class OCAll extends javax.swing.JPanel {

    /** Creates new form EirSummary */
    private Image bg;
    private JTable oCAllTable;
    private JScrollPane scroll;
    public DefaultTableModel model;
    private TableColumn column;
    private NButton print;
    private TextArea filterField;
    private int month, year;
    private ArrayList cList = new ArrayList<String>();

    public OCAll(int month, int year) {
        initComponents();
        this.month = month;
        this.year = year;
        setOpaque(false);
        setLocation(getCenterX(761), getCenterY(460));
        setSize(761, 460);
        setVisible(true);
        setBg();
        initTable();
        initButtons();
//        initFilter();
        showTable();
    }

    public final void setBg(){
        bg = new Image("panelb.png",761,460);

        add(bg,-1);

    }

    public int getCenterX(int x){

        int a = 919/2 - x/2;


        return a;
    }

    public int getCenterY(int x){

        int a = 577/2 - x/2;


        return a;
    }

    public void initTable(){
                oCAllTable = new JTable(model);
		oCAllTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		oCAllTable.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
		oCAllTable.getTableHeader().setFont(new Font("Lucida Grande", Font.PLAIN, 12));
		scroll = new JScrollPane(oCAllTable);
		scroll.setBounds(20,15,721,400);
		add(scroll,0);
               		 
    }

    public void showTable(){
        try {
            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();

            DefaultTableModel model = new DefaultTableModel(){
			    public boolean isCellEditable(int row, int column) {
			    	return false;
				}
			};

            int days;
            switch(month){
                case 4:
                case 6:
                case 9:
                case 11: days = 30;break;
                case 2: if((year%4 == 0 && year%100 != 0) || (year%100 == 0 && year %400 == 0))
                            days = 29;
                        else
                            days = 28;break;
                default: days = 31;

            }
                    model.addColumn("Company");
                for(int i = 1; i<= days; i++ )
                    model.addColumn(i);

                int index = getCustCode();
                String cID = "";




                for(int j = 0; j< index;j++){
                        cID = cList.get(j).toString();
                    String sD="";

                    String qC = "SELECT custName FROM Customer WHERE custCode = "+cID;
                    ResultSet rs = stmt.executeQuery(qC);
                        if(rs.next()){
                        Vector rowData = new Vector () ;
                            rowData.add(rs.getString(1));
                            for(int i = 1; i <= days; i++){
                                sD = year+"-"+month+"-"+i;
                                String query = "SELECT COUNT(*) FROM Rental r WHERE outDate = '"+sD+"' and custID = "+cID;
                                rs = stmt.executeQuery(query);


                                if(rs.next())
                                    rowData.add(rs.getInt(1));
                                else
                                    rowData.add("");
                            }

                            model.addRow(rowData);
                        }
                    }
                
                
            oCAllTable.setModel(model);
            Sql.closeSqlConnection(connection);

        } catch (Exception ex) {
            Logger.getLogger(Chassis.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getData(JTable table, int row_index, int col_index){
        return table.getModel().getValueAt(row_index, col_index).toString();
    }


    public void initButtons(){
        print = new NButton("Print", 579, 422);
        print.setSize(80,25);
        add(print,0);

        buttonlisteners();
    }

    public void buttonlisteners()
    {
        print.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae)
            {
                try {
                    oCAllTable.print();
                } catch(Exception e)
                {

                }
            }
        });
    }

        public void initFilter(){
        Text filter = new Text();

        filter.inText("Filter:", 20, 420);
        add(filter,0);
        filterField = new TextArea();
        filterField.initText(200, 25,70, 418);
        add(filterField,0);

    }


    public int getCustCode(){
        int code = 0;
        int i=0;

          try{
            String cuQuery = "SELECT * FROM customer";

            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();
            ResultSet cs = stmt.executeQuery(cuQuery);



            while(cs.next()){
                cList.add(cs.getString(1));
                i++;
            }


            Sql.closeSqlConnection(connection);
          } catch(Exception ex){
                Logger.getLogger(SurC.class.getName()).log(Level.SEVERE, null, ex);
          }

          System.out.println(i);
          return i;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
